import qs from 'qs';
import axios from 'axios';

const targetUrl = process.env.URL;
console.log(targetUrl);
const urlPrefix='/CarBigData'
const http=axios.create({//config
    baseURL:targetUrl+urlPrefix,
    withCredentials: true,
    timeout:1000 * 30,
    headers: {
        'Content-Type': 'application/json; charset=utf-8'
    },
})
// http.interceptors.request.use(config => {
//     config.headers['cartoken'] = Vue.cookie.get('cartoken') // 请求头带上token
//     return config
// })

http.adornUrl=url=>targetUrl+urlPrefix+url

/**
 * get请求参数处理
 * @param {*} params 参数对象
 * @param {*} openDefultParams 是否开启默认参数?
 */
http.adornParams = (params = {}, openDefultParams = true) => {
    let defaults = {
        't': new Date().getTime()
    }
    return openDefultParams ? {...defaults, ...params} : params
}

/**
 * post请求数据处理
 * @param {*} data 数据对象
 * @param {*} openDefultdata 是否开启默认数据?
 * @param {*} contentType 数据格式
 *  json: 'application/json; charset=utf-8'
 *  form: 'application/x-www-form-urlencoded; charset=utf-8'
 */
http.adornData = (data = {}, openDefultdata = true, contentType = 'json') => {
    let defaults = {
        't': new Date().getTime()
    }
    data = openDefultdata ? {...defaults, ...data} : data
    return contentType === 'json' ? JSON.stringify(data) : qs.stringify(data)
}
export default http
